package com.hcy.schedulingapi.service.mqconsumer;

import com.alibaba.fastjson.JSON;
import com.hcy.common.dto.response.GaoDeGfidsResponseDTO;
import com.hcy.commonn.CommonInfoClient;
import com.hcy.rabbitbasicmodel.enums.Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicutils.utils.RedisUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author huchenying
 * @Description 在Spring启动时初始化mq消费线程
 * @Date 2022/3/15 23:12
 */
@Component
public class MQDispatcherInit implements ApplicationRunner {

    Logger logger = LoggerFactory.getLogger(MQDispatcherInit.class);

    @Autowired
    private CommonInfoClient commonInfoClient;

    @Value("${MQADDR}")
    private String MQADDR;
    @Autowired
    private MQDispatcherOrderConsumer mqDispatcherOrderConsumer;
    @Autowired
    RedisUtils redisUtils;

    @Override
    public void run(ApplicationArguments args) {
        logger.info("初始化派单消费者 start ...");
        List<GaoDeGfidsResponseDTO> allTopic;

        try {
//            redisUtils.hPut("carConfigDetailsMap","gaodeGfids", JSON.toJSONString(gaodeGfids));
            Object o = redisUtils.hGet("carConfigDetailsMap", "gaodeGfids");
            logger.info("config -> {}",ToStringBuilder.reflectionToString(o));
            allTopic = JSON.parseArray(o.toString(), GaoDeGfidsResponseDTO.class);
//            allTopic = new ArrayList<>();
//            for (Object object : objects) {
//                String s1 = JSON.toJSONString(object);
//                GaoDeGfidsResponseDTO gaoDeGfidsResponseDTO = JSON.parseObject(s1, GaoDeGfidsResponseDTO.class);
//                allTopic.add(gaoDeGfidsResponseDTO);
//            }
        } catch (Exception e) {
            logger.error("缓存获取失败 -》 {}", ToStringBuilder.reflectionToString(e));
            ResponseData<List<GaoDeGfidsResponseDTO>> responseData = commonInfoClient.getAllTopic();
            if (responseData.getCode() != Code.Success) {
                logger.error("获取Topic配置失败，初始化派单消费者 终止 ...");
            }
            allTopic = responseData.getData();
        }
        if (allTopic != null) {
            logger.info("获取到配置信息开始初始化");
            for (GaoDeGfidsResponseDTO datum : allTopic) {
                datum.setAddress(MQADDR);
                logger.info("topic init name = [{}]", datum.getGfid());
                Thread thread = new Thread(mqDispatcherOrderConsumer);
                MQDispatcherOrderConsumer.mpInfo.put(thread.getId(), datum);
                thread.setName("派单消费线程 -> " + datum.getGname());
                thread.setDaemon(true);
                thread.start();
            }
            logger.info("初始化完成");
        }
    }
}
